//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.details
//
//import com.intellij.collaboration.ui.VerticalListPanel
//import com.intellij.collaboration.ui.codereview.details.CodeReviewDetailsStatusComponentFactory
//import com.intellij.collaboration.ui.util.bindContentIn
//import com.intellij.collaboration.ui.util.bindTextIn
//import com.intellij.collaboration.ui.util.toAnAction
//import com.intellij.icons.AllIcons
//import com.intellij.icons.ExpUiIcons
//import com.intellij.openapi.actionSystem.DefaultActionGroup
//import com.intellij.openapi.project.Project
//import com.intellij.platform.util.coroutines.childScope
//import com.intellij.ui.ExperimentalUI
//import com.intellij.ui.ScrollPaneFactory
//import com.intellij.ui.components.panels.Wrapper
//import com.intellij.util.ui.JBUI
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.flow.map
//import com.linqingying.gitcode.api.data.GCRepositoryPermissionLevel
//import com.linqingying.gitcode.i18n.GitCodeBundle
//import com.linqingying.gitcode.pullrequest.data.service.GCPRSecurityService
//import com.linqingying.gitcode.pullrequest.ui.details.action.GCPRRemoveReviewerAction
//import com.linqingying.gitcode.pullrequest.ui.details.model.GCPRReviewFlowViewModel
//import com.linqingying.gitcode.pullrequest.ui.details.model.GCPRStatusViewModel
//import com.linqingying.gitcode.ui.avatars.GCAvatarIconsProvider
//import javax.swing.JComponent
//import javax.swing.JScrollPane
//
//internal object GCPRStatusChecksComponentFactory {
//  fun create(
//    parentScope: CoroutineScope,
//    project: Project,
//    reviewStatusVm: GCPRStatusViewModel,
//    reviewFlowVm: GCPRReviewFlowViewModel,
//    securityService: GCPRSecurityService,
//    avatarIconsProvider: GCAvatarIconsProvider
//  ): JComponent {
//    val scope = parentScope.childScope(Dispatchers.Main.immediate)
//    val loadingPanel = createLoadingComponent(scope, reviewStatusVm, securityService)
//    val statusesPanel = VerticalListPanel().apply {
//      add(createAccessDeniedLabel(scope, reviewStatusVm, securityService))
//      add(CodeReviewDetailsStatusComponentFactory.createCiComponent(scope, reviewStatusVm))
//      add(CodeReviewDetailsStatusComponentFactory.createConflictsComponent(scope, reviewStatusVm.hasConflicts))
//      add(CodeReviewDetailsStatusComponentFactory.createRequiredReviewsComponent(scope,
//                                                                                 reviewStatusVm.requiredApprovingReviewsCount,
//                                                                                 reviewStatusVm.isDraft))
//      add(CodeReviewDetailsStatusComponentFactory.createRestrictionComponent(scope,
//                                                                             reviewStatusVm.isRestricted,
//                                                                             reviewStatusVm.isDraft))
//      add(CodeReviewDetailsStatusComponentFactory.createNeedReviewerComponent(scope, reviewFlowVm.reviewerReviews))
//      add(CodeReviewDetailsStatusComponentFactory.createReviewersReviewStateComponent(
//        scope, reviewFlowVm.reviewerReviews,
//        reviewerActionProvider = { reviewer ->
//          DefaultActionGroup(GCPRRemoveReviewerAction(scope, project, reviewFlowVm, reviewer).toAnAction())
//        },
//        reviewerNameProvider = { reviewer -> reviewer.getPresentableName() },
//        avatarKeyProvider = { reviewer -> reviewer.avatarUrl },
//        iconProvider = { iconKey, iconSize -> avatarIconsProvider.getIcon(iconKey, iconSize) }
//      ))
//    }
//    val scrollableStatusesPanel = ScrollPaneFactory.createScrollPane(statusesPanel, true).apply {
//      isOpaque = false
//      horizontalScrollBarPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
//      viewport.isOpaque = false
//    }
//
//    return Wrapper().apply {
//      name = "Status check panel"
//      bindContentIn(scope, reviewStatusVm.mergeabilityState.map { mergeability ->
//        if (mergeability == null) loadingPanel else scrollableStatusesPanel
//      })
//    }
//  }
//
//  private fun createLoadingComponent(
//    scope: CoroutineScope,
//    reviewStatusVm: GCPRStatusViewModel,
//    securityService: GCPRSecurityService
//  ): JComponent {
//    val stateLabel = CodeReviewDetailsStatusComponentFactory.ReviewDetailsStatusLabel("Pull request status: loading label").apply {
//      border = JBUI.Borders.empty(5, 0)
//      icon = if (ExperimentalUI.isNewUI()) ExpUiIcons.Run.TestNotRunYet else AllIcons.RunConfigurations.TestNotRan
//      text = GitCodeBundle.message("pull.request.loading.status")
//    }
//    val accessDeniedLabel = createAccessDeniedLabel(scope, reviewStatusVm, securityService)
//    return VerticalListPanel().apply {
//      name = "Loading statuses panel"
//      add(stateLabel)
//      add(accessDeniedLabel)
//    }
//  }
//
//  private fun createAccessDeniedLabel(
//    scope: CoroutineScope,
//    reviewStatusVm: GCPRStatusViewModel,
//    securityService: GCPRSecurityService
//  ): JComponent {
//    val viewerDidAuthor = reviewStatusVm.viewerDidAuthor
//
//    val mergeForbidden = securityService.isMergeForbiddenForProject()
//    val canMerge = securityService.currentUserHasPermissionLevel(GCRepositoryPermissionLevel.WRITE)
//    val canClose = securityService.currentUserHasPermissionLevel(GCRepositoryPermissionLevel.TRIAGE) || viewerDidAuthor
//    val canMarkReadyForReview = securityService.currentUserHasPermissionLevel(GCRepositoryPermissionLevel.WRITE) || viewerDidAuthor
//
//    return CodeReviewDetailsStatusComponentFactory.ReviewDetailsStatusLabel("Code review status: access denied").apply {
//      border = JBUI.Borders.empty(5, 0)
//      icon = if (ExperimentalUI.isNewUI()) ExpUiIcons.Status.Error else AllIcons.RunConfigurations.TestError
//      bindTextIn(scope, reviewStatusVm.isDraft.map { isDraft ->
//        when {
//          !canClose -> GitCodeBundle.message("pull.request.repo.access.required")
//          !canMarkReadyForReview && isDraft -> GitCodeBundle.message("pull.request.repo.write.access.required")
//          !canMerge && !isDraft -> GitCodeBundle.message("pull.request.repo.write.access.required")
//          mergeForbidden && !isDraft -> GitCodeBundle.message("pull.request.merge.disabled")
//          else -> {
//            isVisible = false
//            return@map ""
//          }
//        }
//      })
//    }
//  }
//}
